<?php
// 上传类型
function uploadType($type)
{
    $arr = [0 => 'admin_unknown', 1 => 'banner', 2=>'naire',3=>'headimg'];
    return isset($arr[$type]) && $arr[$type]?$arr[$type]:$arr[0];
}

function redisConf(){
    return [
        // 缓存主机
        'host'       => config('sysconfig.redis_host'),
        // 缓存端口
        'port'     => config('sysconfig.redis_port'),
        // 缓存密码
        'password'     => '',
        // 缓存数据库
        'select'   => config('sysconfig.redis_select'),
        // 缓存有效期 0表示永久缓存
        'timeout'   => config('sysconfig.redis_timeout'),
        // 缓存前缀
        'prefix'   => config('sysconfig.redis_prefix')
    ];
}

// 获取ip
function getRealIP()
{
    $forwarded = request()->header("x-forwarded-for");
    if ($forwarded) {
        $ip = explode(',', $forwarded)[0];
    } else {
        $ip = request()->ip();
    }
    return $ip;
}
//curl请求
function http_url($url){
    $info = curl_init();
    curl_setopt($info, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($info, CURLOPT_HEADER, 0);
    curl_setopt($info, CURLOPT_NOBODY, 0);
    curl_setopt($info, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($info, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($info, CURLOPT_URL, $url);
    $output = curl_exec($info);
    curl_close($info);
    return $output;
}

/**
 * @param string $url post请求地址
 * @param array $params
 * @return mixed
 */
function curl_post($url, array $params = array())
{
    $data_string = json_encode($params);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt(
        $ch,
        CURLOPT_HTTPHEADER,
        array(
            'Content-Type: application/json'
        )
    );
    $data = curl_exec($ch);
    curl_close($ch);
    return ($data);
}

// 列表转化为树状图
function list_to_tree($list, $root = 0, $pk = 'id', $pid = 'parent_id', $child = 'child') {
	// 创建Tree
   
	$tree = array();
	if (is_array($list)) {
		// 创建基于主键的数组引用
		$refer = array();
		foreach ($list as $key => $data) {
            
			$refer[$data[$pk]] = &$list[$key];
		}
      
		foreach ($list as $key => $data) {
			// 判断是否存在parent
			$parentId = 0;
          
			if (isset($data[$pid])) {
				$parentId = $data[$pid];
			}

			if ((string)$root == $parentId) {
				$tree[] = &$list[$key];
               
			} else {
             
				if (isset($refer[$parentId])) {
					$parent = &$refer[$parentId];
					$parent[$child][] = &$list[$key];

				}
			}
		}
	}
	return $tree;
}